class ImagesController < ApplicationController
  before_filter :authorize, :except => [:index, :list, :display, :show, :create_ecard, :display_ecard, :display_map ]

  def index
    @image = Image.find(:first, :order => "RAND()")
    # display
    # render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @image_pages, @images = paginate :images, :per_page => 10
  end
  
  def display
    code = params[:id]
    if (code == nil || code.size == 0)
      code = "root"
    end
    @currentcategory = Category.find_by_code(code)
    @subcategories = @currentcategory.categories
    @images = @currentcategory.images
    flash[:notice] = 'categories: ' + @subcategories.size.to_s + ' images: ' + @images.size.to_s
  end

  def show
    @image = Image.find(params[:id])
    @comments = @image.comments
    @categories = @image.categories
  end

  def new
    @image = Image.new
  end

  def create
    @image = Image.new(params[:image])
    @user = 
    if @image.save
      flash[:notice] = 'Image was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @image = Image.find(params[:id])
  end

  def update
    @image = Image.find(params[:id])
    if @image.update_attributes(params[:image])
      flash[:notice] = 'Image was successfully updated.'
      redirect_to :action => 'show', :id => @image
    else
      render :action => 'edit'
    end
  end

  def destroy
    Image.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

  def create_ecard
    @image = Image.find(params[:id])
    @user = User.find_by_id(session[:user_id])
    if request.post?
      @code = rand(100000000)
      @ecard = Ecard.new(:user_id => session[:user_id],
                         :image_id => @image[:id],
                         :from_name => params[:from_name],
                         :from_email => params[:from_email],
                         :to_name => params[:to_name],
                         :to_email => params[:to_email],
                         :message => params[:message],
                         :code => @code)
      if @ecard.save
        email = EcardMailer.create_sendcard(params[:from_email], params[:to_name], params[:from_name], params[:to_name], params[:message], @code)
        email.set_content_type("text/html")
        EcardMailer.deliver(email)
      
        adminemail = EcardMailer.create_adminsent(params[:from_email], params[:to_name], params[:from_name], params[:to_name], params[:message], @code)
        adminemail.set_content_type("text/html")
        EcardMailer.deliver(adminemail)
      
        flash[:notice] = "Your Ecard has been sent"
      end
      @comments = @image.comments
      @categories = @image.categories
      render :action => 'show'
    end    
  end
  
  def display_ecard
    @ecard = Ecard.find_by_code(params[:code])
    @image = @ecard.image    
    email = EcardMailer.create_cardread(params[:from_email], @code)
    email.set_content_type("text/html")
    EcardMailer.deliver(email)
  end
  
  def prints
    redirect_to_index unless request.xhr?
  end
  
  def printinfo
  
  end
  
  def stock
  
  end

  def redirect_to_index(msg = nil)
    flash[:notice] = msg if msg
    redirect_to :action => :index
  end
  
  def display_map
  
  end
  
end
